package com.xrkj.b2c.controller.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xrkj.b2c.controller.common.BasicController;
import com.xrkj.b2c.model.sys.SysPrivilege;
import com.xrkj.b2c.service.sys.SysPrivilegeService;
import com.xrkj.b2c.utils.StringUtil;
import com.xrkj.b2c.utils.UUIDUtil;

/**
 * 
 * <B>系统名称：</B><BR>
 * <B>模块名称：</B><BR>
 * <B>中文类名：</B>权限控制器<BR>
 * <B>概要说明：</B><BR>
 * @author （赵子靖）
 * @since 2018年07月22日
 */
@RequestMapping("/sysPrivilege")
@Controller
public class SysPrivilegeController extends BasicController {

    @Autowired
    private SysPrivilegeService privilegeService;

    private Logger logger = Logger.getLogger(SysPrivilegeController.class);

    /**
     * 
     * <B>方法名称：</B>转向管理页面<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author Tom
     * @since 2017年10月9日
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/forwardPrivilege")
    public ModelAndView forwardPrivilege(HttpServletRequest request, HttpServletResponse response) {
        String is = request.getParameter("t");
        request.setAttribute("is", is);
        return new ModelAndView("WEB-INF/pages/sys/sys-privilege/privilege-to-manager");
    }

    /**
     * 
     * <B>方法名称：</B>获取列表<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author perry
     * @since 2017年10月20日
     * @param request
     * @param response
     * @param pageSize
     * @param pageNo
     * @return
     */
    @RequestMapping("/privilegeList")
    @ResponseBody
    public Object privilegeList(HttpServletRequest request, HttpServletResponse response) {
        String search = request.getParameter("search");//名称
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        Map<String, Object> ret = new HashMap<String, Object>();
        Map<String, Object> paramter = new HashMap<String, Object>();
        paramter.put("offset", StringUtil.isEmpty(offset) ? null : Integer.parseInt(offset));
        paramter.put("limit", StringUtil.isEmpty(limit) ? null : Integer.parseInt(limit));
        paramter.put("search", search);
        paramter.put("pParentId", "root");
        Integer total = privilegeService.findPrivilegeTotal(paramter);
        List<SysPrivilege> privileges = new ArrayList<SysPrivilege>();
        if (total > 0) {
            privileges = privilegeService.findPrivilegeList(paramter);
        }
        ret.put("total", total);
        ret.put("rows", privileges);
        return ret;
    }

    /**
     * 
     * <B>方法名称：</B>转向新增方法<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author perry
     * @since 2017年10月20日
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/forwardAddPrivilege")
    public ModelAndView forwardAddPrivilege(HttpServletRequest request, HttpServletResponse response) {
        return new ModelAndView("WEB-INF/pages/sys/sys-privilege/add-privilege-dialog");
    }

    /**
     * 
     * <B>方法名称：</B>新增<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author perry
     * @since 2017年11月6日
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/addPrivilege")
    @ResponseBody
    public Map<String, Object> addPrivilege(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> ret = new HashMap<String, Object>();
        String pParentId = request.getParameter("pParentId");
        String pName = request.getParameter("pName");
        String pCode = request.getParameter("pCode");
        String pStatus = request.getParameter("pStatus");
        String pSort = request.getParameter("pSort");

        if (StringUtil.isEmpty(pName)) {
            ret.put("success", false);
            ret.put("msg", "保存失败，请输入权限名称");
            return ret;
        }
        SysPrivilege privilege = new SysPrivilege();
        privilege.setpId(UUIDUtil.id());
        privilege.setpParentId(pParentId);
        privilege.setpName(pName);
        privilege.setpCode(pCode);
        privilege.setpSort(StringUtil.isEmpty(pSort)?0:Short.parseShort(pSort));
        privilege.setpStatus(StringUtil.isEmpty(pStatus) ? 0 : Short.parseShort(pStatus));
        if ((privilegeService.insertPrivilege(privilege) > 0)) {
            ret.put("success", true);
            ret.put("msg", "保存成功。");
        } else {
            ret.put("success", false);
            ret.put("msg", "保存失败。");
        }
        return ret;
    }

    /**
     * 
     * <B>方法名称：</B>跳转修改页面<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author perry
     * @since 2017年11月6日
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/forwardUpdatePrivilege")
    public ModelAndView forwardUpdatePrivilege(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");//编号
        SysPrivilege privilege = privilegeService.selectByPrimaryKey(id);
        if(privilege!=null){
            privilege.setParentPrivilege(privilegeService.selectByPrimaryKey(privilege.getpParentId()));
        }
        request.setAttribute("item", privilege);
        return new ModelAndView("WEB-INF/pages/sys/sys-privilege/update-privilege-dialog");
    }

    /**
     * 
     * <B>方法名称：</B>根据ID获取信息<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author perry
     * @since 2017年11月8日
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getPrivilegeById")
    @ResponseBody
    public Map<String, Object> getPrivilegeById(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");//编号
        SysPrivilege privilege = privilegeService.selectByPrimaryKey(id);
        Map<String, Object> ret = new HashMap<String, Object>();
        ret.put("item", privilege);
        return ret;
    }

    /**
     * 
     * <B>方法名称：</B>新增<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author perry
     * @since 2017年11月6日
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/updatePrivilege")
    @ResponseBody
    public Map<String, Object> updatePrivilege(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> ret = new HashMap<String, Object>();
        String pId = request.getParameter("pId");
        String pParentId = request.getParameter("pParentId");
        String pName = request.getParameter("pName");
        String pCode = request.getParameter("pCode");
        String pStatus = request.getParameter("pStatus");
        String pSort = request.getParameter("pSort");

        if (StringUtil.isEmpty(pName)) {
            ret.put("success", false);
            ret.put("msg", "保存失败，请输入权限名称");
            return ret;
        }
        
        SysPrivilege privilege = privilegeService.selectByPrimaryKey(pId);
        if (privilege == null) {
            ret.put("success", false);
            ret.put("msg", "保存失败，未获取到修改对象。");
            return ret;
        }
        privilege.setpParentId(pParentId);
        privilege.setpName(pName);
        privilege.setpCode(pCode);
        privilege.setpSort(StringUtil.isEmpty(pSort)?0:Short.parseShort(pSort));
        privilege.setpStatus(StringUtil.isEmpty(pStatus) ? 0 : Short.parseShort(pStatus));
        
        if ((privilegeService.updatePrivilege(privilege)) > 0) {
            ret.put("success", true);
            ret.put("msg", "保存成功。");
        } else {
            ret.put("success", false);
            ret.put("msg", "保存失败。");
        }
        return ret;
    }

    /**
     * 
     * <B>方法名称：</B>删除<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author Tom
     * @since 2017年10月11日
     * @return
     */
    @RequestMapping("/deletePrivilege")
    @ResponseBody
    public Map<String, Object> deletePrivilege(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> ret = new HashMap<String, Object>();
        String id = request.getParameter("id");
        if (privilegeService.deletePrivilege(id) > 0) {
            //删除成功
            ret.put("success", true);
            ret.put("msg", "删除成功。");
        } else {
            //删除失败
            ret.put("success", false);
            ret.put("msg", "删除失败");
        }
        return ret;
    }
    
    /**
     * 
     * <B>方法名称：</B>获取子节点<BR>
     * <B>概要说明：</B><BR>
     * @author perry
     * @since 2017年11月29日 	
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getPrivilegeByParent")
    @ResponseBody
    public Map<String, Object> getPrivilegeByParent(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> ret = new HashMap<String, Object>();
        String pParentId = request.getParameter("pParentId");
        Map<String, Object> paramter = new HashMap<String, Object>();
        paramter.put("pParentId", pParentId);
        List<SysPrivilege> privileges = privilegeService.findPrivilegeList(paramter);
        ret.put("rows", privileges);
        return ret;
    }
    
}
